package com.yckj.activechicken.business.document.service.impl;

import com.yckj.activechicken.business.document.mapper.BusiDocumentMapper;
import com.yckj.activechicken.business.document.model.BusiDocument;
import com.yckj.activechicken.business.document.model.BusiDocumentExample;
import com.yckj.activechicken.business.document.service.IBusiDocumentService;
import com.yckj.activechicken.common.utils.StringUtils;
import com.yckj.activechicken.common.utils.file.FileUtils;
import com.yckj.activechicken.framework.config.ProjectConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.service.impl
 * </p>
 * <p/>
 * <p>
 * Description: 描述【文档管理业务处理层】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/5/15 17:29
 */
@Service
public class BusiDocumentServiceImpl implements IBusiDocumentService {

    @Autowired
    private BusiDocumentMapper busiDocumentMapper;

    @Value("${upload.appendixFile}")
    private String appendixFileFile;

    /**
     * 通过公司id以及文档类型查询文档信息
     *
     * @param busiDocument
     * @return
     */
    @Override
    public List<BusiDocument> queryDocByCompanyId(BusiDocument busiDocument) {
        BusiDocumentExample busiDocumentExample = new BusiDocumentExample();
        busiDocumentExample.createCriteria()
                .andBusiIdEqualTo(busiDocument.getBusiId())
                .andDocTypeEqualTo(busiDocument.getDocType());
        return busiDocumentMapper.selectByExample(busiDocumentExample);
    }

    /**
     * 通过业务id查询文档信息
     * @param busiDocument
     * @return
     */
    @Override
    public List<BusiDocument> queryDocByBusiId(BusiDocument busiDocument) {
        BusiDocumentExample busiDocumentExample = new BusiDocumentExample();
        busiDocumentExample.createCriteria()
                .andBusiIdEqualTo(busiDocument.getBusiId())
                .andDocTypeEqualTo(busiDocument.getDocType());
        return busiDocumentMapper.selectByExample(busiDocumentExample);
    }

    /**
     * 通过文档信息删除文档
     *
     * @param busiDocument
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delDocById(BusiDocument busiDocument) {
        int num = busiDocumentMapper.deleteByPrimaryKey(busiDocument.getDocId());
        if (num > 0) {
            //删除磁盘文件
            FileUtils.deleteFile(ProjectConfig.getProfile() + busiDocument.getDocUrl());
        }
        return num;
    }

    /**
     * 添加文档信息，上传文档
     *
     * @param busiDocument
     * @return
     */
    @Override
    public Map<String, String> addDocument(BusiDocument busiDocument) {
        Map<String, String> outMap = new HashMap<>(2);

        //处理多文件上传问题
        List<Map<String, String>> outList = FileUtils.uploadFileList(busiDocument.getAppendixFile(), this.appendixFileFile);
        if (StringUtils.isNotEmpty(outList)) {
            for (Map<String, String> map : outList) {
                if ("true".equals(map.get("result"))) {
                    //添加文档记录
                    busiDocument.setDocName(map.get("fileName"));
                    busiDocument.setDocLocalName(map.get("localFileName"));
                    busiDocument.setDocMimeType(map.get("mimeType"));
                    busiDocument.setDocUrl(map.get("uploadPath"));
                    busiDocumentMapper.insertSelective(busiDocument);
                    outMap.put("result", "true");
                } else {
                    outMap.put("result", "false");
                    outMap.put("message", map.get("message"));
                }
            }
        }else {
            outMap.put("result", "false");
            outMap.put("message", "公司信息创建失败，请联系管理员！");
        }
        return outMap;
    }
}

